package com.lkm.app_ui;

import android.app.Dialog;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.lkm.app_ui.help.OnSteelyardKeyEventHelp;
import com.lkm.app_ui.log.SystemLogger;
import com.lkm.app_ui.ui.MainActivity;

import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;



public class DialogWrapFragment extends DialogFragment implements FragmentManager.OnBackStackChangedListener {



    protected Fragment createFragment(Bundle savedInstanceState){
        return fragment;
    }

    private Fragment fragment;



    public static DialogWrapFragment getInstance(Fragment fragment, boolean likeDialog) {
        DialogWrapFragment f = new DialogWrapFragment();
        Bundle data = new Bundle();
        data.putBoolean("likeDialog",likeDialog);
        f.setArguments(data);
        f.fragment = fragment;
        return f;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if(getArguments().getBoolean("likeDialog",false)){
            return inflater.inflate(R.layout.dialog_content, container, false);
        }else {
            return inflater.inflate(R.layout.content, container, false);
        }

    }





















    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        Fragment f = getChildFragmentManager().findFragmentById(R.id.content);
        if (f != null) {
        } else {
            getChildFragmentManager().beginTransaction().replace(R.id.content, createFragment(savedInstanceState)).addToBackStack(null).commit();
        }
        getChildFragmentManager().addOnBackStackChangedListener(this);

        hideBottomUIMenu();
    }

    @Override
    public void onBackStackChanged() {
        if (getChildFragmentManager().getBackStackEntryCount() == 0) {
            SystemLogger.UI.onDialogFragmentNotUI();
            dismiss();
        }

    }


    private Handler mainHandler;
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mainHandler = new Handler(Looper.getMainLooper());
        Dialog dialog = new Dialog(getActivity(), R.style.AppTheme_dialogFreamentWrap){
            @Override
            public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
                OnSteelyardKeyEventHelp.onSendKeyFormK(keyCode,mainHandler);
                return super.onKeyDown(keyCode, event);
            }

            @Override
            public boolean dispatchKeyEvent(@NonNull KeyEvent event) {
                boolean is = MainActivity.dispatchKeyEvent_(event);
                if(!is)return super.dispatchKeyEvent(event);
                return is;
            }
        };

        if (RunTimeData.isSecondSimulate) {
            DisplayMetrics outMetrics = new DisplayMetrics();
            dialog.getWindow().getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
            int widthPixels = outMetrics.widthPixels;
            dialog.getWindow().setLayout(widthPixels / 2, ViewGroup.LayoutParams.MATCH_PARENT);
            dialog.getWindow().setGravity(Gravity.TOP | Gravity.LEFT);
        }
//        dialog.getWindow().setBackgroundDrawableResource(R.color.bg_launch);
        dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        return dialog;
    }

    @Override
    public void onResume() {
        super.onResume();
        hideBottomUIMenu();
    }



    protected void hideBottomUIMenu() {
        hideBottomUIMenu(this.getDialog().getWindow());
    }


    public static void hideBottomUIMenu(Window window) {

        if(RunTimeData.isXIANGCHENG){

            return;
        }


        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) {
            View v = window.getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {

            View decorView = window.getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
            decorView.setSystemUiVisibility(uiOptions);
        }
    }

}
